﻿using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebAppRazor.Pages.Employees;

// [BindProperties(SupportsGet = true)]
public class Edit : PageModel
{
    [BindProperty(SupportsGet = true)]
    public int? Id { get; set; }
    [BindProperty]
    public InputModel? InputModel { get; set; }
    
    
    public IActionResult OnGet()
    {
        return Page();
    }

    
    public IActionResult OnPostSave()
    {
        if (!ModelState.IsValid)
        {
            this.InputModel!.ErrorMessages = GetErrors();
            return Page();
        }
        // do something with the input model
        return RedirectToPage("Index");
    }

    public IActionResult OnPostDelete()
    {
        
        return RedirectToPage("Index");
    }

    private List<string> GetErrors()
    {
        List<string> errorMessages = new List<string>();
        foreach (var item in ModelState.Values)
        {
            foreach (var error in item.Errors)
            {
                errorMessages.Add(error.ErrorMessage);
            }
        }

        return errorMessages;
    }
}

public class InputModel
{
    
    public int? Id { get; set; }
    [Required]
    public string? Name { get; set; }
    
    public List<string>? ErrorMessages { get; set; }
    
}